package com.woniu.service.impl;

import com.woniu.dao.RoleDao;
import com.woniu.dao.UserDao;
import com.woniu.dao.UserroleDao;
import com.woniu.pojo.*;
import com.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserroleDao userroleDao;
    @Autowired
    private RoleDao roleDao;
    @Override
    public User login(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUnameEqualTo(user.getUname());
        criteria.andUpwdEqualTo(user.getUpwd());
        List<User> userList = userDao.selectByExample(userExample);
        return userList.size()==0?null:userList.get(0);
    }

    @Override
    public List<User> getUsersByPage() {
        UserExample userExample = new UserExample();
        List<User> userList = userDao.selectByExample(userExample);
        return userList;
    }

    @Override
    public List<User> getUsersByPage(PageBean pageBean,User user) {
        //封装PageBean的信息
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if(user.getUaccount()!=null&&!user.getUaccount().equals("")) {
            criteria.andUaccountLike("%"+user.getUaccount()+"%");
        }

        Long countRow = userDao.countByExample(userExample);
        pageBean.setCountRow(countRow.intValue());
        Integer countPage = pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);
        //分页进行查询
        userExample.setOffset((long) ((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        userExample.setLimit(pageBean.getPageSize());

        List<User> userList = userDao.selectByExample(userExample);

        return userList;
    }

    @Override
    public void deleteSelected(Integer[] uids) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUidIn(Arrays.asList(uids));
        userDao.deleteByExample(userExample);
    }

    @Override
    public User getUserByUid(Integer uid) {
        User user = userDao.selectByPrimaryKey(uid);
        return user;
    }

    @Override
    public void editUser(User user) {
        userDao.updateByPrimaryKeySelective(user);
    }

    @Override
    public Map<String, Object> assignRoleShow(Integer uid) {
        //获得已分配的角色
        /**
         * 根据uid在userrole找到多个rid
         * 根据多个rid在角色表中找到对应的对象
         */
        UserroleExample userroleExample = new UserroleExample();
        UserroleExample.Criteria userroleExampleCriteria = userroleExample.createCriteria();
        userroleExampleCriteria.andUidEqualTo(uid);
        List<Userrole> userroleList = userroleDao.selectByExample(userroleExample);
        List<Integer> ridList = new ArrayList<>();
        for(Userrole userrole:userroleList){
            ridList.add(userrole.getRid());
        }
        
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria roleExampleCriteria = roleExample.createCriteria();
        roleExampleCriteria.andRidIn(ridList);
        List<Role> assignRoleList = roleDao.selectByExample(roleExample);

        //获得未分配的角色
        RoleExample roleExample2 = new RoleExample();
        RoleExample.Criteria roleExampleCriteria2 = roleExample2.createCriteria();
        roleExampleCriteria2.andRidNotIn(ridList);
        List<Role> unassignRoleList = roleDao.selectByExample(roleExample2);

        Map<String,Object> assignRoleMap = new HashMap<String,Object>();
        assignRoleMap.put("assignRoleList",assignRoleList);
        assignRoleMap.put("unassignRoleList",unassignRoleList);
        return assignRoleMap;
    }
}
